﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Common;
using System.Collections.Generic;

namespace DAL
{
     [Serializable]
     public class TEPulseData
     {

         public const string TEIDField ="@TEID";

         public const string TIMEField ="@time";

         private String _TEID;

         private DateTime _time;

         public String TEID
         {
             get{ return _TEID;}
             set{ _TEID=value;}
         }
         public DateTime time
         {
             get{ return _time;}
             set{ _time=value;}
         }
         public TEPulseData()
         {
             _TEID="";
             _time=DateTime.Now;
         }
     }

     public class TEPulseDataFactory
     {
         public static TEPulseData Construct(IDataReader reader)
         {
             TEPulseData data = new TEPulseData();

             try{  data.TEID = Convert.ToString(reader["TEID"])  ;} catch { } 

             try{  data.time = Convert.ToDateTime(reader["time"])  ;} catch { } 

             return data;
         }
         public static TEPulseData Construct(DataTable dt)
         {
             return Construct(dt,0);
         }
         public static TEPulseData Construct(DataTable dt,int rowIndex)
         {
             TEPulseData data = new TEPulseData();

             if(dt.Rows.Count<rowIndex+1)

                 return null;

             DataRow dr=dt.Rows[rowIndex];

             if(dr==null)

                 return null;

             try{ data.TEID = Convert.ToString(dr["TEID"]) ;} catch { } 

             try{ data.time = Convert.ToDateTime(dr["time"]) ;} catch { } 

             return data;
         }
         public static TEPulseData Construct(DataRow dr)
         {
             TEPulseData data = new TEPulseData();

             try{ data.TEID = Convert.ToString(dr["TEID"]) ;} catch { } 

             try{ data.time = Convert.ToDateTime(dr["time"]) ;} catch { } 

             return data;
         }
     }
     public class TEPulseDataDBOption
     {
         public static int Insert(TEPulseData d)
         {
             StringBuilder sql=new StringBuilder();
             sql.Append("insert into TEPulse (");
             sql.Append("TEID, ");
             sql.Append("time");
             sql.Append(") values (");
             sql.Append("'");
             sql.Append(d.TEID.ToString());
             sql.Append("'");
             sql.Append(", ");
             sql.Append("'");
             sql.Append(d.time.ToString("yyyy-MM-dd HH:mm:ss"));
             sql.Append("'");
             sql.Append(")");
             return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
         }
         public static int Update(TEPulseData d)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("update TEPulse set ");
             sql.Append("time=");
             sql.Append("'");
             sql.Append(d.time.ToString("yyyy-MM-dd HH:mm:ss"));
             sql.Append("'");             
             sql.Append(" where TEID= '");
             sql.Append(d.TEID.ToString());
             sql.Append("'");  
             return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
         }
         public static int Update(TEPulseData d,DateTime dt)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("update TEPulse set ");
             sql.Append("time=");
             sql.Append("'");
             sql.Append(d.time.ToString("yyyy-MM-dd HH:mm:ss"));
             sql.Append("'");
             sql.Append(" where TEID= '");
             sql.Append(d.TEID.ToString());
             sql.Append("' and time='"+dt.ToString()+"'");
             return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
         }
         public static DataTable DataTableSelect()
         {
             StringBuilder sql=new StringBuilder();
             sql.Append("select * from TEPulse ");
             return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
         }
         public static DataSet DataSetSelect()
         {
             StringBuilder sql=new StringBuilder();
             sql.Append("select * from TEPulse ");
             return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
         }
         public static SqlDataReader ReaderSelect(TEPulseData d)
         {
             StringBuilder sql=new StringBuilder();
             sql.Append("select * from TEPulse ");
             return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
         }
         public static TEPulseData CheckTE(string id)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("select * from TEPulse where TEID= '" + id + "' and convert(varchar(10),[time],120) = '" + DateTime.Now.ToString("yyyy-MM-dd") + "'");
             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
             if (dt.Rows.Count != 0)
             {
                 return TEPulseDataFactory.Construct(dt.Rows[0]);
             }
             return null;
         }
         public static List<TEInfoPart> GetOnlineTE(string id)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("SELECT Workers2TE.WorkersID, TEInfo.zhuceID, TEInfo.shiyongdanwei, TEInfo.bufangweizhi, TELastPulse.time,TEInfo.ID ");
             sql.Append("FROM Workers2TE INNER JOIN TELastPulse ON Workers2TE.TEID = TELastPulse.TEID INNER JOIN TEInfo ON TELastPulse.TEID = TEInfo.ID ");
             sql.Append("WHERE ((Workers2TE.WorkersID = '" + id + "') and ");
             sql.Append("(TELastPulse.time between '" + DateTime.Now.AddMinutes(-16).ToString() + "' and '" + DateTime.Now.ToString() + "'))");
             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
             if (dt.Rows.Count != 0)
             {
                 List<TEInfoPart> ret = new List<TEInfoPart>();
                 for (int i = 0; i < dt.Rows.Count;i++ )
                 {
                     TEInfoPart te = new TEInfoPart();
                     te.bufangweizhi = dt.Rows[i]["TEInfo.bufangweizhi"].ToString();
                     te.LastPulse = dt.Rows[i]["TEPulse.time"].ToString();
                     te.shiyongdanwei = dt.Rows[i]["TEInfo.shiyongdanwei"].ToString();
                     te.zhuCeID = dt.Rows[i]["TEInfo.zhuceID"].ToString();
                     te.TEID = dt.Rows[i]["TEInfo.ID"].ToString();
                 }
                 return ret;
             }
             return null;
         }
         public static List<TEInfoPart> GetOfflineTE(string id)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("SELECT Workers2TE.WorkersID, TEInfo.zhuceID, TEInfo.shiyongdanwei, TEInfo.bufangweizhi, TELastPulse.time,TEInfo.ID ");
             sql.Append("FROM Workers2TE INNER JOIN TELastPulse ON Workers2TE.TEID = TELastPulse.TEID INNER JOIN TEInfo ON TELastPulse.TEID = TEInfo.ID ");
             //sql.Append("WHERE ((Workers2TE.WorkersID = '" + id + "'))");
             sql.Append("WHERE ((Workers2TE.WorkersID = '" + id + "') and ");
             sql.Append("(TELastPulse.time < '" + DateTime.Now.AddMinutes(-16).ToString() + "'))");
             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
             if (dt.Rows.Count != 0)
             {
                 List<TEInfoPart> ret = new List<TEInfoPart>();
                 for (int i = 0; i < dt.Rows.Count; i++)
                 {
                     TEInfoPart te = new TEInfoPart();
                     te.bufangweizhi = dt.Rows[i]["bufangweizhi"].ToString();
                     te.LastPulse = dt.Rows[i]["time"].ToString();
                     te.shiyongdanwei = dt.Rows[i]["shiyongdanwei"].ToString();
                     te.zhuCeID = dt.Rows[i]["zhuceID"].ToString();
                     te.TEID = dt.Rows[i]["ID"].ToString();
                     ret.Add(te);
                 }
                 return ret;
             }
             return null;
         }
         public static void InitLastPulse(string id)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("INSERT INTO TELastPulse (TEID, time) VALUES('"+id+"', '1997-1-1')");             
             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());             
         }
         public static void UpdateLastPulse(string id)
         {
             StringBuilder sql = new StringBuilder();
             sql.Append("UPDATE TELastPulse SET time = '"+DateTime.Now.ToString()+"' WHERE (TEID = '"+id+"')");
             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
         }
     }
}